import Config from './config'
import Util from './util'

// 与页面dom操作变化相关
const Interfaces: any = {
  // 参与者的信息 牌当前总点数， 牌当前张数，当前余额
  roleInfo: {
    // 庄家
    banker: {
      cardPoint: Config.actor.banker.cardPoint,
      cardAmount: 0,
      // 初始金额
      balance: Config.actor.banker.initMoney,
    },
    // 玩家
    player: {
      cardPoint: Config.actor.player.cardPoint,
      cardAmount: 0,
      // 初始金额
      balance: Config.actor.player.initMoney,
    },
  },
  // 发牌
  divCards(
    $banker,
    $player,
    $countB,
    $countP,
    $mask,
    $promot,
    $balance,
    $bankerBalance
  ) {
    // 创建4个li并添加到ul
    let i = 0,
      $li,
      card,
      val,
      result;
    // 遍历四次
    for (i; i < 4; i++) {
      // 如果是第三次(庄家)
      if (i == 2) {
        // 添加一个li标签到庄家的牌堆列表中
        $li = Util.appToEle("li", $banker);
      } else if (i == 3) {
        // 如果是第四次(庄家),添加一个li标签到庄家的牌堆列表中
        $li = Util.appToEle("li", $banker);
        // 并将庄家牌的第二张牌设为未知牌
        $li.className = "unknow";
      } else {
        // 添加两个li标签到玩家的牌堆列表中
        $li = Util.appToEle("li", $player);
      }
      // 拿到随机牌名称
      card = Util.randCard();
      // 拿到牌数
      val = Util.cardVal(card);
      // 给标签添加指定class,渲染对应卡牌
      $li.className = $li.className != "" ? $li.className : card;
      // 将牌数记录到标签上
      $li.setAttribute("data-val", val);
      // 将排名记录到标签上
      $li.setAttribute("data-str", card);
      // 计算点数
      if (i < 2) {
        // 如果发牌到用户
        Util.updateRoleInfo($player, $countP, "player");
        // 每加一张牌判断是否越界
        result = Util.overFlow("Player", $balance, $banker, $bankerBalance);
        if (result) {
          this.promotMes($mask, $promot, false, result);
        }
      } else if (i >= 2) {
        // 更新参与者信息
        Util.updateRoleInfo($banker, $countB, "banker");
        // 判断是否越界
        result = Util.overFlow("Dealer", $balance, $banker, $bankerBalance);
        if (result) {
          this.promotMes($mask, $promot, false, result);
        }
      }
    }
    //判断当前玩家是不是"黑杰克"
    if (
      Interfaces.roleInfo.player.cardAmount == 2 &&
      Interfaces.roleInfo.player.cardPoint == 21
    ) {
      Interfaces.stopCard(
        "Dealer",
        $banker,
        $countB,
        $mask,
        $promot,
        $balance,
        $banker,
        $bankerBalance
      );
    }
  },
  // 提示框的隐藏或展现，flag标记为false表示不隐藏
  promotMes($mask, $promot, flag, mes) {
    //检查玩家当前余额是否为0
    if (Interfaces.roleInfo.player.balance == 0) {
      mes = mes + "<br>This game is over! Player's current balance is 0!";
    }
    if (Interfaces.roleInfo.banker.bankerBalance == 0) {
      mes = mes + "<br>This game is over! The player has obtained all the money of the dealer!";
    }
    // 标志位true关闭提示框
    if (flag) {
      $mask.className = "mask hide";
      return;
    }

    $promot.innerHTML = mes;
    //判断当前是哪种提示框
    if (mes.length < 50) {
      $promot.className += " result";
    } else {
      $promot.className = "promot";
    }
    $mask.className = "mask block";
  },
  // 加一张牌
  addCard(
    currole,
    $role,
    $count,
    $mask,
    $promot,
    $balance,
    $banker,
    $bankerBalance
  ) {

    let $li, card, val, sum, role, result;
    $li = Util.appToEle("li", $role);
    card = Util.randCard();
    val = Util.cardVal(card);
    $li.className = card;
    $li.setAttribute("data-val", val);
    $li.setAttribute("data-str", card);

    if (currole == "Dealer") {
      role = "banker";
    } else {
      role = "player";
    }

    Util.updateRoleInfo($role, $count, role);

    result = Util.overFlow(currole, $balance, $banker, $bankerBalance);
    if (result) {
      this.promotMes($mask, $promot, false, result);
    }
  },
  // 停牌方法
  stopCard(
    currole,
    $role,
    $countB,
    $mask,
    $promot,
    $balance,
    $banker,
    $bankerBalance
  ) {
    let $li, over, result;
    // 背过去的图片反转，计算点数判断越界
    $li = $role.lastElementChild;
    $li.className = $li.dataset.str;

    Util.updateRoleInfo($role, $countB, "banker", false);

    // 如果庄家的点数和大于或等于19
    if (this.roleInfo.banker.cardPoint >= 19) {
      result = Util.overFlow(currole, $balance, $banker, $bankerBalance);

      if (result) {
        this.promotMes($mask, $promot, false, result);
      }

      return;
    }

    // 系统自动加牌
    Interfaces.timer = setInterval(function () {
      Interfaces.addCard(
        currole,
        $role,
        $countB,
        $mask,
        $promot,
        $balance,
        $banker,
        $bankerBalance
      );
    }, 500);
  },
};

export default Interfaces;
